package com.superzlc.miniweb.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import com.superzlc.miniweb.comn.Configuation;
import com.superzlc.miniweb.comn.Util;
import com.superzlc.miniweb.servlet.Model;
import com.superzlc.miniweb.servlet.anno.Controller;
import com.superzlc.miniweb.servlet.anno.QueryParam;
import com.superzlc.miniweb.servlet.anno.RequestMapping;
import com.superzlc.miniweb.servlet.anno.ResponseBody;


@Controller
@RequestMapping(value = "/markfile")
public class ModifyMarkFile {

	private File getMarkFile() {
		String path = Configuation.get("markfile");
		File file = new File(path);
		if (!file.isFile())
			return null;
		if (file.length() > 1024 * 1024)
			return null;
		return file;
	}

	// 重新加载
	@RequestMapping(value = "/reload")
	@ResponseBody
	public String reload(HttpServletResponse response, Model model) throws IOException{
		File file = getMarkFile();
		if (file == null) {
			return null;
		}

		return Util.readString(file);
	}

	// 保存
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(HttpServletResponse response, @QueryParam("content") String content, Model model) {
		System.out.println(content);
		content = content == null ? "" : content;
		File file = getMarkFile();
		if (file == null) {
			return "false";
		}
		try {
			Util.wirteString(file, content);
			return "true";
		} catch(IOException e) {
			return "false";
		}
	}

}
